package com.smyh.UIM.device;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.smyh.UIM.R;
import com.smyh.cache.UIM.MainCahce;
import com.smyh.common.MinaClientThread;
import com.smyh.model.device.DeviceStateInfoModel;
import com.smyh.protcol.device.CMDUtil;
import com.smyh.server.ToastShowMsgThread;
import com.smyh.util.HexStrUtil;

import org.apache.mina.core.buffer.IoBuffer;

/**
 * Created by Administrator on 2016/7/27.
 */
public class DeviceStateFragment extends Fragment{

    /**
     * 模拟量组件
     */
    private EditText runElec01,batVolt02,faultElec03,lineTem04,earthElecFiled05,fetchSelfVolt06,
            stateInfo07,faultInfo8;
    /**
     * 遥信量组件
     */
    private EditText shortCut09, earthFault11, batError;
    private EditText shortCutTimer10, earthFaultTime,batErrorTime;
    private Button deviceStateReadBtn01;

    static Handler handler;
    /**
     * @param inflater
     * @param container
     * @param savedInstanceState
     * @return
     */
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.activity_device_state,container,false);
        handler = new Handler(){
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);

                DeviceStateInfoModel.freshDeviceStateData();
            }
        };
        initView(view);
        initEvent();
        return view;
    }

    private void initEvent() {
        deviceStateReadBtn01.setOnClickListener(readBtnListener);
    }

    /**
     * 刷新遥测数据：刷新当前选择设备数据
     * @param
     */
    public static void freshMockState(){
        handler.sendMessage(new Message());
    }

    private void initView(View view){

        runElec01 = (EditText) view.findViewById(R.id.runElec01);
        batVolt02 = (EditText) view.findViewById(R.id.batVolt02);
        faultElec03 = (EditText) view.findViewById(R.id.faultElec03);
        lineTem04 = (EditText) view.findViewById(R.id.lineTem04);
        earthElecFiled05 = (EditText) view.findViewById(R.id.earthElecFiled05);
        fetchSelfVolt06 = (EditText) view.findViewById(R.id.fetchSelfVolt06);
        stateInfo07 = (EditText) view.findViewById(R.id.stateInfo07);
        faultInfo8 = (EditText) view.findViewById(R.id.faultInfo8);

        shortCut09 = (EditText) view.findViewById(R.id.shortCut09);
        earthFault11 = (EditText) view.findViewById(R.id.earthFault11);
        batError = (EditText) view.findViewById(R.id.batError);

        shortCutTimer10 = (EditText) view.findViewById(R.id.shortCutTimer10);
        earthFaultTime = (EditText) view.findViewById(R.id.earthFaultTime);
        batErrorTime = (EditText) view.findViewById(R.id.batErrorTime);

        deviceStateReadBtn01 = (Button) view.findViewById(R.id.deviceStateReadBtn01);

        DeviceStateInfoModel.initDeviceStateComp(runElec01,batVolt02,faultElec03,lineTem04,earthElecFiled05,
                fetchSelfVolt06,stateInfo07,faultInfo8,shortCut09, earthFault11,
                batError,shortCutTimer10, earthFaultTime,batErrorTime);

    }

    private View.OnClickListener readBtnListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(MinaClientThread.session != null && MinaClientThread.session.isConnected()){
                switch (v.getId()){
                    case R.id.deviceStateReadBtn01:{


                        IoBuffer buf = CMDUtil.controlByType(HexStrUtil
                                .hexStr2Bytes(MainCahce.chooseDeviceAddr), (byte) 6, (byte) 0);
                        MinaClientThread.session.write(buf);

                        Toast.makeText(getContext(),"下发查询指示器状态指令!",Toast.LENGTH_SHORT).show();
                    }
                }
            }else {

                new Thread(new ToastShowMsgThread("通讯异常！请检查")).start();
            }
        }
    };

}
